package com.example.hospital_208.controller;

import com.example.hospital_208.po.MedicalRecord;
import com.example.hospital_208.service.MedicalRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/medical-records")
@Tag(name = "病历管理接口", description = "医疗病历信息的增删改查操作，字段与数据库表严格对应")
public class MedicalRecordController {

    private final MedicalRecordService medicalRecordService;

    public MedicalRecordController(MedicalRecordService medicalRecordService) {
        this.medicalRecordService = medicalRecordService;
    }

    // 查询所有病历
    @GetMapping
    @Operation(summary = "查询所有病历", description = "获取系统中全部病历信息，包含 record_id、patient_id 等字段")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = MedicalRecord.class)))
    })
    public List<MedicalRecord> getAllMedicalRecords() {
        return medicalRecordService.listMedicalRecords();
    }
    // 根据 user_id 查询病历
    @GetMapping("/user/{userId}")
    @Operation(summary = "根据 user_id 查询病历", description = "通过用户 ID（user_id）获取对应的病历信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = MedicalRecord.class))),
            @ApiResponse(responseCode = "404", description = "未找到对应病历")
    })
    public MedicalRecord getMedicalRecordByUserId(
            @PathVariable("userId")
            @Parameter(description = "用户 ID（user_id）", example = "1", required = true)
            Integer userId) {
        return medicalRecordService.getMedicalRecordByUserId(userId);
    }
    // 根据 ID 查询病历
    @GetMapping("/{recordId}")
    @Operation(summary = "根据 ID 查询病历", description = "通过病历 ID（record_id）获取详细信息")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(schema = @Schema(implementation = MedicalRecord.class))),
            @ApiResponse(responseCode = "404", description = "病历不存在")
    })
    public MedicalRecord getMedicalRecord(
            @PathVariable("recordId")
            @Parameter(description = "病历 ID（record_id）", example = "1", required = true)
            Integer recordId) {
        return medicalRecordService.getMedicalRecordById(recordId);
    }

    // 新增病历
    @PostMapping
    @Operation(summary = "新增病历", description = "向系统中添加新病历，需传入 patient_id、diagnosis 等字段数据")
    @ApiResponses({
            @ApiResponse(responseCode = "201", description = "添加成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误，字段未正确传入")
    })
    public String addMedicalRecord(
            @RequestBody
            @Parameter(description = "病历实体对象，包含patient_id、diagnosis 等字段",
                    content = @Content(schema = @Schema(implementation = MedicalRecord.class)))
            MedicalRecord medicalRecord) {
        int result = medicalRecordService.addMedicalRecord(medicalRecord);
        return result > 0 ? "病历添加成功" : "病历添加失败";
    }

    // 修改病历
    @PutMapping
    @Operation(summary = "修改病历信息", description = "更新已有病历的信息，依据 record_id 匹配，修改 patient_id等字段")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "修改成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或病历不存在")
    })
    public String updateMedicalRecord(
            @RequestBody
            @Parameter(description = "包含更新信息的病历实体，需正确设置 record_id 及其他要修改的字段",
                    content = @Content(schema = @Schema(implementation = MedicalRecord.class)))
            MedicalRecord medicalRecord) {
        int result = medicalRecordService.editMedicalRecord(medicalRecord);
        return result > 0 ? "病历信息修改成功" : "病历信息修改失败";
    }

    // 删除病历
    @DeleteMapping("/{recordId}")
    @Operation(summary = "删除病历", description = "根据病历 ID（record_id）删除病历记录")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "删除成功",
                    content = @Content(schema = @Schema(type = "string"))),
            @ApiResponse(responseCode = "400", description = "参数错误或病历存在关联数据")
    })
    public String deleteMedicalRecord(
            @PathVariable("recordId")
            @Parameter(description = "待删除的病历 ID（record_id）", example = "2", required = true)
            Integer recordId) {
        int result = medicalRecordService.removeMedicalRecord(recordId);
        return result > 0 ? "病历删除成功" : "病历删除失败";
    }
}